/*
 * main.cpp
 *
 * Copyright 2013 wcd <wcd@asus>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 */

#include "_report_.h"
#include "_stock_.h"

int main()
{
    char x;
    char key;
    int date_d,date_m,date_y;
    float count,trade_price,price_sale;

    _stock_ sclad;

    do
    {
        std::cout<< "\n"
                    " 1 - Add new record\n"
                    " 2 - Display\n"
                    " 3 - Save\n"
                    " 4 - Load\n"
                    " 5 - Sale\n"
                    " 6 - Print history\n"
                    " 7 - Load history\n"
                    " 8 - Save history\n"
                    " 0 - Exit\n";
        std::cin>>key;
        switch(key)
        {
        case '1':
        {
            do
            {
                printf("Enter the date in format DD.MM.YYYY\n");
                scanf("%d.%d.%d",&date_d,&date_m,&date_y);

            }while(date_d > 31 or date_d <= 0 or date_m > 12 or date_m <= 0 or date_y > 3000 or date_y <= 1800);

            printf("Enter the count and purchase price\n");
            scanf("%f%f",&count,&trade_price);

            do
            {
                printf("Do you want to keep the sale price?\nYes - 1, No - 0\n");
                std::cin>>x;
                if (x == '0')
                {
                    if(!sclad.addNext(date_d,date_m,date_y,count,trade_price,trade_price))
                    {
                        printf("Error add item\n");
                        exit(0);
                    }
                }

                if (x == '1')
                {
                    do
                    {
                        printf("Enter purchase price\n");
                        scanf("%f",&price_sale);
                    }while(price_sale < trade_price);

                    if(!sclad.addNext(date_d,date_m,date_y,count,trade_price,price_sale))
                    {

                        printf("Error add item\n");
                        exit(0);
                    }
                }
            }while(x != '0' and x != '1');

        }

        case '2':
        {
            sclad.print();
            break;
        }
        case '3':
        {
            if(sclad.save())
                std::cout<<"Save ok :)\n";
            else
                std::cout<<"Save error :(\n";
            break;
        }

        case '4':
        {
            std::cout<<(sclad.loadBase() ? "load base ok :)\n" :"Error load base\n");
            break;
        }

        case '5':
        {

            do
            {
                printf("Enter the date in DD.MM.YYYY\n");
                scanf("%d.%d.%d",&date_d,&date_m,&date_y);

            }
            while(date_d > 31 or
                  date_d <= 0 or
                  date_m > 12 or
                  date_m <= 0 or
                  date_y > 3000 or
                  date_y <= 1800);

            printf("Enter the count\n");
            scanf("%f",&count);

            switch(sclad.search(date_d,date_m,date_y,count))
            {
            case 0:
                printf("ERROR\n");
                break;
            case 1:
                printf("THE OPERATION WAS SUCCESSFUL\n");
                break;

            default:
                printf("Error! quantity with the current date is less than desired.\n");
                break;
            };
        }
            break;
        case '6':
            sclad.printHistory();
            break;
        case '7':
            sclad.loadHistory();
            break;
        case '8':
            sclad.saveHistory();
            break;
        }
    }
    while(key != '0');

    return 0;
}
